import { ListResult, PageParams, PageResult, Result } from '@/data/result';
import { post } from './request';

// 查询类，只提供数据和 success 标识
export async function tablePage(url: string, params: PageParams) {
  const { current: pageNum, pageSize, ...data } = params;
  const { page, success }: PageResult = await post(`${url}/page`, {
    pageNum,
    pageSize,
    data,
  });
  return {
    //兼容 proTable
    data: page?.list,
    total: page?.total,
    success,
  };
}

export async function page(url: string, params: PageParams) {
  const { current: pageNum, pageSize, ...data } = params;
  const { page, success }: PageResult = await post(`${url}/page`, {
    pageNum,
    pageSize,
    data,
  });
  return {
    list: page?.list,
    total: page?.total,
    success,
  };
}

export async function list(url: string, params?: any) {
  const { list, success }: ListResult = await post(`${url}/list`, params || {});
  return {
    list,
    success,
  };
}

export async function get(url: string, id: string) {
  const { data, success }: Result = await post(`${url}/one`, id);
  return { data, success };
}

// 通用查询
export async function query<T>(url: string, params?: any) {
  const { data, success }: any = await post<T>(`${url}`, params || {});
  return { data, success };
}

// 提交类会返回 code，success 方便业务判断

// 通用提交
export async function submit<T>(url: string, params?: any) {
  return await post<T>(`${url}`, params || {});
}

export async function update<T>(url: string, params: any) {
  return await post<T>(`${url}/update`, params);
}

export async function add<T>(url: string, params: any) {
  return await post<T>(`${url}/add`, params);
}

export async function remove<T>(url: string, id: string) {
  return await post<T>(`${url}/delete`, id);
}
